package session.stateless.CRM;

import entity.CRM.Discount;
import javaclass.ProductType;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class DiscountBean implements DiscountBeanLocal {
    @PersistenceContext()
    private EntityManager em;
    
    @Override
    public Long createDiscount(ProductType productType, Long lowerLimit, Long higherLimit, Double discountRate) {
        Discount discount = new Discount();
        discount.setProductType(productType);
        discount.setLowerLimit(lowerLimit);
        discount.setHigherLimit(higherLimit);
        discount.setDiscountRate(discountRate);
        
        em.persist(discount);
        return discount.getDiscountId();
    }

    @Override
    public void modifyDiscount(Long discountId, Long lowerLimit, Long higherLimit, Double discountRate) {
        Discount discount =em.find(Discount.class, discountId);
        
        if (discount ==null)
            System.out.println("Discount does not exist.");
        else
        {
            discount.setLowerLimit(lowerLimit);
            discount.setHigherLimit(higherLimit);
            discount.setDiscountRate(discountRate);
            
            em.flush();
        }
        
    }

    @Override
    public void deleteDiscount(Long discountId) {
        Discount discount =em.find(Discount.class, discountId);  
        if (discount==null)
            System.out.println("Discount does not exist.");
        else
        {
            em.remove(discount);
            em.flush();
        }
    }

    @Override
    public Double calculateDiscountRate(ProductType productType, Long realQuantity) {
        Query query = em.createQuery("SELECT p FROM Discount p WHERE p.productType = :productType");
        if(query!=null){
            query.setParameter("productType", productType);


            for (Object o: query.getResultList())
            {
                Discount temp_discount = (Discount) o;
                if(( realQuantity >= temp_discount.getLowerLimit() ) && ( temp_discount.getHigherLimit()> realQuantity ) )
                    return temp_discount.getDiscountRate();
            }
            return new Double(0);
        }else{
            return new Double(0);
        }
    }

    @Override
    public List<Discount> getDiscounts() {
        Query query =em.createQuery("SELECT p from Discount p");
        return query.getResultList();
    }

    @Override
    public boolean checkConflict(ProductType productType, Long lowerLimit, Long higherLimit) {
        Query q=em.createQuery("SELECT c FROM Discount c WHERE c.productType =:productType");
        q.setParameter("productType", productType);
        List<Discount> temp =q.getResultList();
        
        for (Discount discount:temp)
        {
            if ((discount.getHigherLimit()>lowerLimit)&(discount.getLowerLimit()<higherLimit))
                return true;            
        }
        return false;
    }
    
    public List<Discount> getDiscountsByType(ProductType productType) {
        Query q=em.createQuery("SELECT c FROM Discount c WHERE c.productType=:productType");
        q.setParameter("productType", productType);
        return q.getResultList();
    }
}
